package com.bestcem.xm.qdes.convert.grpc;

import com.alibaba.fastjson.JSONArray;
import com.bestcem.bp.xm.user.grpc.v1.services.User;
import com.bestcem.xm.deliver.grpc.v1.services.WechatDeliverInfo;
import com.bestcem.xm.qdes.entity.QdesProject;
import com.bestcem.xm.qdes.entity.*;
import com.bestcem.xm.qdes.grpc.client.dto.SurveyRespondentDto;
import com.bestcem.xm.qdes.grpc.client.dto.UserDto;
import com.bestcem.xm.qdes.grpc.client.dto.WechatDeliverInfoDto;
import com.bestcem.xm.qdes.grpc.v1.services.*;
import com.bestcem.xm.survey.grpc.v1.services.Respondent;
import org.mapstruct.*;

import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Optional;

@Mapper(componentModel = MappingConstants.ComponentModel.SPRING,
        collectionMappingStrategy = CollectionMappingStrategy.ADDER_PREFERRED,
        nullValueCheckStrategy = NullValueCheckStrategy.ALWAYS,
        uses = {BaseConvertMapper.class},
        imports = {NameListNtype.class, DateTimeFormatter.class, QStructStatus.class, ProjectStatus.class, ProjectPtype.class, QuotaQuotaType.class, ProjectEntryCategory.class, ProjectEntryPermType.class, ProjectEntryStatus.class, ProjectEntryBadgeStatus.class})
public interface QdesGrpcConvertMapper {

    //@Mapping(target = "status", expression = "java(organization.getStatusValue())")
    //@Mapping(target = "source", expression = "java(organization.getSourceValue())")
    //@Mapping(target = "footer", expression = "java(organization.getFooterValue())")
    //@Mapping(target = "deliverCheck", source = "deliverAudit")
    //@Mapping(target = "complete", source = "complete")
    //OrgInfoDto toOrgInfoDto(com.bestcem.bp.xm.user.grpc.v1.services.Organization organization);

    @Mapping(target = "status", expression = "java(QStructStatus.forNumber(qstruct.getStatus()))")
    QStruct toGrpcQStruct(QdesQstruct qstruct);

    @BeforeMapping
    default void toGrpcProjectBefore(QdesProject project) {
        Optional.ofNullable(project).map(QdesProject::getGroupList).ifPresent(ary -> project.setGroupListList(JSONArray.parseArray(ary, String.class)));
    }

    @Mapping(target = "status", expression = "java(ProjectStatus.forNumber(project.getStatus()))")
    @Mapping(target = "ptype", expression = "java(ProjectPtype.forNumber(project.getPtype()))")
    @Mapping(target = "updatedDt", expression = "java(project.getUpdatedDt().format(DateTimeFormatter.ofPattern(\"yyyy/MM/dd HH:mm:ss\")))")
    Project toGrpcProject(QdesProject project);


    @BeforeMapping
    default void toGrpcQuotaBefore(QdesQuota quota) {
        Optional.ofNullable(quota).map(QdesQuota::getQuotaCondList).ifPresent(ary -> quota.setQuotaCondListList(JSONArray.parseArray(ary, String.class)));
    }

    @Mapping(target = "quotaType", expression = "java(QuotaQuotaType.forNumber(quota.getQuotaType()))")
    Quota toGrpcQuota(QdesQuota quota);

    Question toGrpcQuestion(QdesQuestion question);

    @Mapping(target = "status", expression = "java(user.getStatus().getNumber())")
    @Mapping(target = "edition", expression = "java(user.getEdition().getNumber())")
    @Mapping(target = "userIndex", expression = "java(user.getIndex().getNumber())")
    @Mapping(target = "mobile", expression = "java(user.getPhone())")
    UserDto toUserDto(com.bestcem.bp.xm.user.grpc.v1.services.User user);

    List<UserDto> toUserDtoList(List<User> usersList);

    WechatDeliverInfoDto toWechatDeliverInfoDto(WechatDeliverInfo info);

    @BeforeMapping
    default void toGrpcProjectEntryBefore(QdesProjectEntry item) {
        Optional.ofNullable(item).map(QdesProjectEntry::getPermissionsArry).ifPresent(ary -> item.setPermissionsList(JSONArray.parseArray(ary, String.class)));
    }


    @Mapping(target = "category", expression = "java(ProjectEntryCategory.forNumber(item.getCategory()))")
    @Mapping(target = "permType", expression = "java(ProjectEntryPermType.forNumber(item.getPermType()))")
    @Mapping(target = "status", expression = "java(ProjectEntryStatus.forNumber(item.getStatus()))")
    @Mapping(target = "badgeStatus", expression = "java(ProjectEntryBadgeStatus.forNumber(item.getBadgeStatus()))")
    ProjectEntry toGrpcProjectEntry(QdesProjectEntry item);

    ProjectExtraAttr toGrpcProjectExtraAttr(QdesProjectExtraAttr item);

    @Mapping(target = "ntype", expression = "java(NameListNtype.forNumber(item.getNtype()))")
    NameList toGrpcNameList(QdesNameList item);

    @Mapping(target = "dataStatus", expression = "java(respondent.getDataStatus().getNumber())")
    @Mapping(target = "status", expression = "java(respondent.getStatus().getNumber())")
    @Mapping(target = "source", expression = "java(respondent.getSource().getNumber())")
    @Mapping(target = "clientType", expression = "java(respondent.getClientType().getNumber())")
    SurveyRespondentDto toSurveyRespondentDto(Respondent respondent);

    List<SurveyRespondentDto> toSurveyRespondentDtoList(List<Respondent> items);
}
